import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { resolve } from 'path';
import typescript from '@rollup/plugin-typescript';

function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}

export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    typescript({
      include: ['src/types/**/*', 'src/index.ts'],
      outDir: 'dist',
    }),
  ],
  esbuild: {
    exclude: ['src/types/**/*', 'public/*'],
  },
  resolve: {
    alias: [
      // /@/xxxx => src/xxxx
      {
        find: /\/@\//,
        replacement: pathResolve('src') + '/',
      },
      // /#/xxxx => types/xxxx
      {
        find: /\/#\//,
        replacement: pathResolve('types') + '/',
      },
    ],
    extensions: ['.js', '.vue', '.json', '.ts'],
  },
  css: {
    preprocessorOptions: {
      sass: {
        javascriptEnabled: true,
      },
    },
  },

  build: {
    minify: 'esbuild',
    lib: {
      entry: resolve(__dirname, './src/index.ts'),
      name: 'KtwlProcess',
      fileName: (format) => `KtwlProcess.${format}.js`,
    },
    rollupOptions: {
      external: ['element-plus', 'vue', '@element-plus/icons-vue'],
      output: {
        exports: 'named',
        globals: {
          'element-plus': 'ElementPlus',
          '@element-plus/icons-vue': 'ElementPlusIconsVue',
          vue: 'vue',
        },
      },
    },
    emptyOutDir: false,
  },
});
